<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% page_section_static ||= nil %>
<% page_section = page_section_static || nil %>
<div class="section <%= "read_only" if (page_section && (page_section["attachment_id"] || page_section["submission_id"])) %>" id="page_section_<%= idx %>">
  <% if page_section %>
  <div class="section_content">
      <% if !page_section["section_type"] || page_section["section_type"] == "rich_text" || page_section["section_type"] == "html" %>
        <%= raw(page_section["content"]) || t('rich_text.default_content', "Nothing Entered Yet") %>
      <% elsif page_section["section_type"] == "attachment" %>
        <div style="text-align: center;" class="attachment">
          <% attachment = @static_attachments.find{|a| a.id == page_section["attachment_id"]} %>
          <% if attachment && attachment.content_type.match(/\Aimage/) %>
            <a href="<%= attachment.unencoded_filename %>"><img src="<%= attachment.unencoded_filename %>" alt="<%= attachment.unencoded_filename %>"/></a>
          <% elsif attachment %>
            <a href="<%= attachment.unencoded_filename %>">
              <%= t('links.download_attachment', "Click here to Download *%{attachment}*",
                    :attachment => attachment.display_name,
                    :wrapper => '<span class="filename">\1</span>') %>
            </a>
          <% end %>
        </div>
      <% elsif page_section["section_type"] == "submission" %>
        <% submission = @submissions_hash[page_section["submission_id"]] rescue nil %>
        <% rendered = false %>
        <% if submission %>
          <div style="margin-bottom: 10px;">
            <%= t('headers.submission', "Student Submission for %{assignment}, created %{created_date}",
                  :assignment => submission.assignment.title,
                  :created_date => datetime_string(submission.submitted_at || submission.graded_at || submission.created_at)) %>
          </div>
          <% if submission.submission_type == 'online_url' %>
            <a href="<%= submission.url %>">{{#t}}View the URL submitted for this assignment{{/t}}</a>
            <% rendered = true %>
          <% elsif submission.submission_type = 'online_upload' %>
            <ul>
            <% submission.attachments.each do |a| %>
              <% attachment = @static_attachments.find{|att| att.id == a.id } %>
              <% if attachment %>
                <li><a href="<%= attachment.unencoded_filename %>">
                  <%= t('links.download_attachment', "Click here to Download *%{attachment}*",
                        :attachment => a.display_name,
                        :wrapper => '<span class="filename">\1</span>') %>
                </a></li>
              <% end %>
            <% end %>
            </ul>
            <% rendered = true %>
          <% elsif submission.submission_type = 'online_text_entry' %>
            <div style="margin: 10px 20px;">
              <%= submission.body %>
            </div>
            <% rendered = true %>
          <% end %>
        <% end %>
        <% if !rendered %>
          {{#t}}There used to be a submission here, but we couldn't find it or didn't support the submission type for exporting.{{/t}}
        <% end %>
      <% end %>
  </div>
  <% end %>
</div>
